/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.webservice.ws;

import com.ipcc.manager.entity.IPCC_EXTENSIONS;
import com.ipcc.manager.model.IPCC_EXTENSION_GROUPSModel;
import com.ipcc.other.error.ERRORS;
import com.ipcc.webservice.model.ExtensionWSModel;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author Administrator
 */
@WebService(serviceName = "ExtensionWS")
public class ExtensionWS {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "createXMLExtension")
    public String createXMLExtension(@WebParam(name = "ie") IPCC_EXTENSIONS ie) {
        try {
            if (ExtensionWSModel.checkFileExists(ie.getExtension() + ".xml")) {
                return ERRORS.getDetailError(ERRORS.EXIST_XML);
            }
            String strTemp = ExtensionWSModel.getLinesInTemplate("templateXML.txt");
            strTemp = strTemp.replaceAll("<user_id>", ie.getExtension());
            strTemp = strTemp.replaceAll("<account_code>", ie.getExtension());
            strTemp = strTemp.replaceAll("<password>", ie.getPassword());
            strTemp = strTemp.replaceAll("<vm-password>", ie.getExtension());
            strTemp = strTemp.replaceAll("<extension>", ie.getExtension());
            strTemp = strTemp.replaceAll("<group>", IPCC_EXTENSION_GROUPSModel.getNameById(ie.getIpcc_extension_groups().getGroup()));
            ExtensionWSModel.createXML(strTemp, ie.getExtension() + ".xml");
        } catch (Exception e) {
            e.printStackTrace();
            return ERRORS.getDetailError(ERRORS.ERROR_INTERNAL);
        }
        return ERRORS.getDetailError(ERRORS.SUCCESS);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "editPasswordXMLExtension")
    public String editPasswordXMLExtension(@WebParam(name = "ie") IPCC_EXTENSIONS ie) {
        if (!ExtensionWSModel.checkFileExists(ie.getExtension() + ".xml")) {
            return ERRORS.getDetailError(ERRORS.NON_EXIST_XML);
        }
        try {
            ExtensionWSModel.editPassInXML(ie.getPassword(), ie.getExtension() + ".xml");
            return ERRORS.getDetailError(ERRORS.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ERRORS.getDetailError(ERRORS.ERROR_INTERNAL);
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "deleteXMLExtension")
    public String deleteXMLExtension(@WebParam(name = "fileName") String fileName) {
        if(ExtensionWSModel.delXMLFile(fileName)) return ERRORS.getDetailError(ERRORS.SUCCESS);
        return ERRORS.getDetailError(ERRORS.FAIL);
    }
}
